package com.orangelife.mobile.pay.biz;
import android.content.Context;

import com.orangelife.mobile.individual.entity.WeChat;
import com.orangelife.mobile.pay.interfaze.IWeChat;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 微信实现类
 */
public class WeChatImpl implements IWeChat {
    private PayReq req;
    private IWXAPI msgApi;
    private Context context;
    private WeChat weChat;
    @Override
    public void setParameter(Context context, WeChat weChat) {
         this.context = context;
         this.weChat = weChat;
         initAPI();
         genPayReq();
         //调起微信支付
         sendPayReq();
    }

    private void initAPI() {
        req = new PayReq();
        if(msgApi==null) {
            msgApi = WXAPIFactory.createWXAPI(context, weChat.getAppID());
        }
        msgApi.registerApp(weChat.getAppID());
    }

    private void genPayReq() {
        req.appId = weChat.getAppID();
        req.partnerId = weChat.getPartner();
        req.prepayId = weChat.getPrepayID();
        req.packageValue = weChat.getPackageName();
        req.nonceStr = weChat.getNonceStr();
        req.timeStamp = weChat.getTimeStamp();
        req.sign = weChat.getSign();
    }

    private void sendPayReq() {
        msgApi.registerApp(weChat.getAppID());
        msgApi.sendReq(req);
    }

}
